package com.shopping.mall.core.module.order.controller;

import com.alibaba.excel.EasyExcel;
import com.shopping.mall.auth.annotation.UnLogin;
import com.shopping.mall.base.http.Result;
import com.shopping.mall.core.module.order.entity.Delivery;
import com.shopping.mall.core.module.order.entity.Order;
import com.shopping.mall.core.module.order.entity.OrderDelivery;
import com.shopping.mall.core.module.order.service.DeliveryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

@Api("快递单号")
@RestController
@RequestMapping("/order/delivery")
public class DeliveryController {
    @Autowired
    private DeliveryService deliveryService;

    @ApiOperation("修改订单快递信息")
    @PostMapping
    public Result<Order> setDelivery(@RequestBody OrderDelivery orderDelivery){
        return deliveryService.setDelivery(orderDelivery);
    }

    @UnLogin
    @ApiModelProperty("下载模板")
    @GetMapping("/excel")
    public void getExcel(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("快递单号", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), Delivery.class).sheet("sheet1").doWrite(null);
    }

    @UnLogin
    @ApiOperation("批量导入")
    @PostMapping("/excel")
    public Result<Object> batchSetDelivery(@RequestParam("file")MultipartFile file){
        return deliveryService.batchSetDelivery(file);
    }

}
